import java.util.ArrayList;
import java.util.Scanner;

public class BookSystem {
    public static void main(String[] args){
        ArrayList<Book> list = new ArrayList<>();
        Scanner cin = new Scanner(System.in);
        loop:
        while(true){
                System.out.println("1:添加图书");
                System.out.println("2:显示图书");
                System.out.println("3:查询图书");
                System.out.println("4:删除图书");
                System.out.println("5:修改图书");
                System.out.println("6:退出");
                System.out.println("请输入您的选择：");
                String ops = cin.next();
            switch (ops) {
                case "1" -> addBook(list);
                case "2" -> showBook(list);
                case "3" -> queryBook(list);
                case "4" -> deleteBook(list);
                case "5" -> updateBook(list);
                case "6" -> {
                    System.out.println("退出");
                    // break loop;
                    System.exit(0); // 停止虚拟机运行
                }
                default -> System.out.println("没有这个选项");
            }
        }
    }
    public static void addBook(ArrayList<Book> list){
        // 利用空参构造学生对象
        Book s = new Book();
        Scanner cin = new Scanner(System.in);
        String id = null;
        while(true){
            System.out.println("请输入图书id");
            id = cin.next();
            boolean flag = contains(list, id);
            if(flag){
                System.out.println("id已经存在，请重新输入");
            }else{
                s.setId(id);
                break;
            }
        }

        System.out.println("请输入图书名");
        String name = cin.next();
        s.setName(name);

        System.out.println("请输入学生的作者");
        String author = cin.next();
        s.setAuthor(author);

        System.out.println("请输入图书的单价");
        int price = cin.nextInt();
        s.setPrice(price);

        list.add(s);

        System.out.println("图书信息添加成功");
    }
    public static void showBook(ArrayList<Book> list){
        if(list.isEmpty()){
            System.out.println("当前无图书信息，请添加后再查询");
            return;
        }
        System.out.println("id\t\tname\t\tauthor\t\tprice");
        for(int i = 0; i < list.size(); i ++){
            System.out.println(list.get(i).getId() + "\t\t" + list.get(i).getName() + "\t\t" + list.get(i).getAuthor() + "\t\t" + list.get(i).getPrice());
        }
    }
    public static void queryBook(ArrayList<Book> list){
        if(list.isEmpty()){
            System.out.println("当前无图书信息，请添加后再查询");
            return;
        }
        Scanner cin = new Scanner(System.in);
        System.out.println("请输入需要查询的图书名称");
        String cur = cin.next();
        int inx = getIndex_name(list, cur);
        if(inx == -1){
            System.out.println("暂无该书信息");
            return;
        }
        System.out.println(list.get(inx).getId() + "\t\t" + list.get(inx).getName() + "\t\t" + list.get(inx).getAuthor() + "\t\t" + list.get(inx).getPrice());
    }
    public static void deleteBook(ArrayList<Book> list){
        Scanner cin = new Scanner(System.in);
        System.out.println("请输入要删除的id");
        String id = cin.next();
        int inx = getIndex(list, id);
        if(inx < 0){
            System.out.println("id不存在，删除失败");
            return;
        }
        list.remove(inx);
        System.out.println("id-" + id + "的图书删除成功");
    }
        public static void updateBook (ArrayList<Book> list){
        Scanner cin = new Scanner(System.in);
        System.out.println("请输入要修改的图书id");
        String id = cin.next();
        int inx = getIndex(list, id);
    //        System.out.println("cur time : " + inx);
        if(inx == -1){
            System.out.println("要修改的id-" + id + "不存在");
            return;
        }
        Book cur = list.get(inx);

        System.out.println("请输入要修改的图书名称");
        String name = cin.next();
        cur.setName(name);

        System.out.println("请输入要修改的图书作者");
        String author = cin.next();
        cur.setAuthor(author);

        System.out.println("请输入要修改的图书价格");
        int price = cin.nextInt();
        cur.setPrice(price);

        list.set(inx, cur);

        System.out.println("图书信息修改成功");
    }
    public static boolean contains(ArrayList<Book> list, String id){
        return getIndex(list, id) >= 0;
    }
    public static int getIndex(ArrayList<Book> list, String id){
        for(int i = 0; i < list.size(); i ++){
            if(list.get(i).getId().equals(id)) return i;
        }
        return -1;
    }
    public static int getIndex_name(ArrayList<Book> list, String name){
        for(int i = 0; i < list.size(); i ++){
            if(list.get(i).getName().equals(name)) return i;
        }
        return -1;
    }
}